import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-05-31
 * Time: 19:13
 */
@WebServlet("/show")//没有分号~

public class ShowRequest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StringBuilder result = new StringBuilder();
        result.append(req.getProtocol());//版本号
        result.append("<br>");

        result.append(req.getMethod());
        result.append("<br>");

        result.append(req.getRequestURI());
        result.append("<br>");

        result.append(req.getQueryString());//无字符串
        result.append("<br>");

        result.append(req.getContextPath());
        result.append("<hr>");

        Enumeration<String> headerNames = req.getHeaderNames();
        //用不了for each语法，因为这个集合类没继承那个集合接口，也不是数组~
        //而其本身，也可以看做是自身的迭代器
        while(headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String value = req.getHeader(name);
            result.append(name);
            result.append(": ");
            result.append(value);
            result.append("<br>");
        }
        result.append("<hr>");

        Enumeration<String> keys = req.getParameterNames();
        while(keys.hasMoreElements()) {
            String key = keys.nextElement();
            String value = req.getParameter(key);
            result.append(key);
            result.append("=");
            result.append(value);
            result.append("<br>");
        }
        result.append("<hr>");
        String value1 = req.getParameter("a");
        String value2 = req.getParameter("b");
        String value3 = req.getParameter("c");
        result.append(value1 == null ? "noFound" : value1);
        result.append("<br>");
        result.append(value2 == null ? "noFound" : value2);
        result.append("<br>");
        result.append(value3 == null ? "noFound" : value3);
        result.append("<br>");

        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write(result.toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        //获取请求的body
        StringBuilder result = new StringBuilder();
        Enumeration<String> keys = req.getParameterNames();
        while(keys.hasMoreElements()) {
            String key = keys.nextElement();
            String value = req.getParameter(key);
            result.append(key);
            result.append("=");
            result.append(value);
            result.append("<br>");
        }
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write(result.toString());
    }
}
